//
//  GiftLogic.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/12.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "GiftLogic.h"
#import "GiftModel.h"
#import "VMsgMessage.h"
#import "MsgLogic.h"
@implementation GiftLogic

-(void)getGiftListHookData:(giftGetSuccess)success
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"get_gift_list"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //[param setObject:@"" forKey:@""];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {

    } success:^(id responseObject) {
        NSArray<GiftModel *> *model = [NSArray modelArrayWithClass:[GiftModel class] json:[responseObject valueForKey:@"list"]];
        success(model);
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}

-(void)sendGiftWith:(GiftModel *)model AndNumber:(NSInteger)number Hook:(giftSend)hook
{
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"deal_api" A:@"send_gift"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:SafeStr(_touid) forKey:@"to_user_id"];
    [param setObject:StringFromInt(number) forKey:@"count"];
    [param setObject:StringFromInt(model.id) forKey:@"gid"];
    [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"发送中")];
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        [[HUDHelper sharedInstance] syncStopLoading];
        //扣费成功，发送礼物消息
        NSDictionary *dic = [responseObject valueForKey:@"send"];
        UnifiedMessage *sendmodel = [UnifiedMessage modelWithDictionary:dic];
        sendmodel.type = CY_CHAT_GIFT;
        sendmodel.conversationDesc = ASLocalizedString(@"[礼物]");
        sendmodel.deviceType = @"IOS";
        
        NSData *data = [sendmodel modelToJSONData];
        TIMConversation * conversation = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:self.touid];
        TIMCustomElem * jsondata = [[TIMCustomElem alloc] init];
        jsondata.data = data;
        TIMMessage * msg = [[TIMMessage alloc] init];
        [msg addElem:jsondata];
        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"礼物消息发送成功")];

        [conversation sendMessage:msg succ:^{
            hook(YES,nil,msg);
        } fail:^(int code, NSString *msg) {
            hook(NO,msg,nil);
            [MBProgressHUD showTopTipMessage:msg];
        }];

        
        
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] syncStopLoading];

        hook(NO,error,nil);
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}

@end
